//
//  CashierNewSearchViewController.m
//  TryShopping
//
//  Created by LiYinDi on 16/6/7.
//  Copyright © 2016年 RuiBen. All rights reserved.
//

#import "CashierNewSearchViewController.h"
#import "CashierNewSearchCell.h"

#import "TheLoginViewController.h"

@interface CashierNewSearchViewController ()
{
    NSMutableArray *modelArray;
    NSUInteger _PageIndex;
    NSUInteger _TotalCount;
    NSUInteger _TotalPage;
}
@end

@implementation CashierNewSearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    _searchTextField.text=_keyWordStr;
    _searchTextField.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X
    [self setAutoLayout];
    modelArray=[NSMutableArray array];
    _PageIndex=0;
   [self loadItemNextPage:_PageIndex];
    // Do any additional setup after loading the view from its nib.
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    [modelArray removeAllObjects];
    _PageIndex=0;
    [self loadItemNextPage:_PageIndex];
    return YES;
}

-(void) loadItemNextPage:(NSUInteger)oldPageNum {
    if(oldPageNum!=0 && _TotalCount<=modelArray.count)
        return;
    if(oldPageNum == 0){
        [modelArray removeAllObjects];
        [_tableview reloadData];
    }
    _PageIndex=oldPageNum+1;
    [self requestItemList: _PageIndex pageSize:6];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return modelArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 112;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * const  ResultTableViewCellIdentifier = @"CashierNewSearchCell";
    CashierNewSearchCell *cell=( CashierNewSearchCell *)[tableView dequeueReusableCellWithIdentifier: ResultTableViewCellIdentifier];
    if (cell==nil) {
        NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"CashierNewSearchCell" owner:self options:nil];
        cell = [array objectAtIndex:0];
    }
    
    cell.model=modelArray[indexPath.row];
//    if (indexPath.row == [modelArray count] - 1)
//    {
//        if (_PageIndex<=_TotalPage) {
//            //  NSLog(@"###LOADING???");
//            [self loadItemNextPage:_PageIndex];
//        }
//    }

    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.navigationController popViewControllerAnimated:YES];
    CashierNewSearchDataModel *searchmodel=modelArray[indexPath.row];
    if ([self.delegate respondsToSelector:@selector(CashierNewSearchCellCliCK:)]) {
        
        [self.delegate CashierNewSearchCellCliCK:searchmodel];
    }
}
-(void)requestItemList:(NSUInteger)pageIndex pageSize:(NSUInteger)pageSize
{
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* Token = [user objectForKey:@"tokenId"];
    
    [Global showIndeterminateHud:@"加载中，请稍后。。。" delay:30 view:self.view];
    NSMutableDictionary *params=[[NSMutableDictionary alloc] init];
    [params setObject:Token forKey:@"tokenId"];
    NSNumber *pageSizeNumber = [NSNumber numberWithInteger:pageSize];
    NSNumber *pageIndexNumber = [NSNumber numberWithInteger:pageIndex];
    [params setObject:pageSizeNumber forKey:@"PageSize"];
    [params setObject:pageIndexNumber forKey:@"PageIndex"];
    
    [params setObject:_searchTextField.text forKey:@"sword"];
    [HttpRequest requestData:@"Product/Search" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
        
        if (isSuccess) {
                 NSLog(@"******===%@",severDataDict.dataDict);
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [Global hideProgressHUDInternal:self.view];
                _TotalCount=[[severDataDict.dataDict objectForKey:@"TotalCount"] integerValue];
          
                if (_TotalCount%6!=0) {
                    _TotalPage=_TotalCount/6+1;
                }
                else if (_TotalCount%6==0)
                {
                    _TotalPage=_TotalCount/6;
                }
                NSMutableArray * mItemsArray=[NSMutableArray arrayWithArray:[severDataDict.dataDict objectForKey:@"DataLine"]];
                for (NSDictionary *dic in mItemsArray) {
                    CashierNewSearchDataModel *dataModel=[CashierNewSearchDataModel dataModelWithDict:dic];
                    [modelArray addObject:dataModel];
                    
                }
                [_tableview reloadData];
                
                
            }
            else
            {
                [Global hideProgressHUDInternal:self.view];
                NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                //  [Global showTextHud:requestResultStr view:self.view];
                [Global showTextHudSub:requestResultStr view:self.view];
                
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==9999) {
                    
                    TheLoginViewController *loginVC=[[TheLoginViewController alloc] init];//2.0{9个参数 少2个}
                    [self.navigationController pushViewController:loginVC animated:YES];
                }
                
            }
        }
        else
        {
            [Global hideProgressHUDInternal:self.view];
            [Global showTextHudSub:severDataDict.resultStr view:self.view];
        }
    }];
    
}

- (IBAction)backAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)setAutoLayout
{
    
    [_topLineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left) ;
        make.right.equalTo(self.view.mas_right) ;
        make.height.equalTo(@1) ;
        make.bottom.equalTo(self.view.mas_top).with.offset(64);
    }];
    [_searchParentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_backBtn.mas_right);
        make.height.equalTo(@35);
        make.right.equalTo(self.view.mas_right).with.offset(-40);
        make.bottom.equalTo(_topLineLabel.mas_top).with.offset(-5);
    }];
    [_backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_searchParentView.mas_centerY);
        make.height.equalTo(@30);
        make.width.equalTo(@50);
        make.left.equalTo(self.view.mas_left);
    }];
    [_searchImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_searchParentView.mas_left).with.offset(5);
        make.height.equalTo(@30);
        make.width.equalTo(@30);
        make.centerY.equalTo(_searchParentView.mas_centerY);
        
    }];
    [_searchTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_searchImageView.mas_right);
        make.height.equalTo(@35);
        make.right.equalTo(_searchParentView.mas_right);
        make.centerY.equalTo(_searchParentView.mas_centerY);
    }];
    [_tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.bottom.equalTo(self.view.mas_bottom);
        make.top.equalTo(_topLineLabel.mas_bottom);
          }];

}

@end
